package com.zdy.syn;

/**
 * Created by 逸足天涯
 * on 3/9/2020.
 */
public class UnsageBank {
    public static void main(String[] args) {
        Acount marryMoney = new Acount(100, "结婚基金");
        Drawing me = new Drawing(marryMoney,50,"me");
        Drawing girlFriend = new Drawing(marryMoney,100,"girlFriend");
        me.start();
        girlFriend.start();
    }
}
//账户
class Acount{
     int money;
     String name;
    public Acount(int money,String name){
        this.money = money;
        this.name = name;
    }
}
//去银行取款
class Drawing extends Thread{
    //账户
    private Acount acount;
    //取款金额
    private int drawingMoney;
    //取款人
    private String name;
    //构造函数注入取款信息
    public Drawing(Acount acount,int drawingMoney,String name){
        this.acount = acount;
        this.drawingMoney = drawingMoney;
        this.name = name;
    }
    //取钱
    @Override
    public  void run() {
        synchronized (acount){
            if((acount.money- drawingMoney) < 0){
            System.out.println("你好"+name+"，你们余额不足");
            return;
        }
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            acount.money-=drawingMoney;
            System.out.println(name+ "取款：" +drawingMoney);
            System.out.println("账户余额为："+acount.money);
        }
    }

}

